package tv.youxing.android.application;

import android.os.Environment;

import com.anupcowkur.reservoir.Reservoir;
import com.lzy.okgo.OkGo;
import com.lzy.okserver.OkDownload;
import com.mob.MobApplication;
import com.mob.MobSDK;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.squareup.leakcanary.RefWatcher;
import com.xiasuhuei321.loadingdialog.manager.StyleManager;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.utils.ImageLoaderConfig;

/**
 * Created by apple on 2017/4/17.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .application
 * 文件名：   BaseApplication
 * 创建者：   叶克武
 * 创建时间： 2017/4/17 下午2:59
 * 描述：    TODO
 */
public class BaseApplication extends MobApplication {

    public static BaseApplication Instance;

    public static int current_Pos = 0;

    public static final String SD_PATH = Environment
            .getExternalStorageDirectory().getAbsolutePath() + "/youxing/";

    // 缓存图片路径
    public static final String BASE_IMAGE_CACHE = SD_PATH + "cache/images/";


    // 视频缓存地址
    public static final String BASE_VIDEO_CACHE = SD_PATH + "video/";



    /**
     * 网络视频的播放进度
     */
    public static Map<Integer, Integer> mVideoPos = new HashMap<Integer, Integer>();


    /**
     * 本地视频播放进度
     */
    public static Map<Integer, Integer> mLocalVideoPos = new HashMap<Integer, Integer>();


    /**
     * 最新一条播放记录
     */
    public static VideoPlayerPostModel mNewsPlay;


    /**
     * 内存检漏
     */
    private RefWatcher refWatcher;





    @Override
    public void onCreate() {
        super.onCreate();
        Instance = this;


        initLibrary();

    }


    //第三方库的初始化
    void initLibrary() {

        //mob
        MobSDK.init(this, this.a(), this.b());


        //缓存库
        try {
            Reservoir.init(this, 1024 * 1024); //in bytes 1M
        } catch (Exception e) {
            //failure
        }

        //下载库
        OkGo.getInstance().init(this);
        OkDownload.getInstance().
                setFolder(SD_PATH + "/youxing/video/");//视频存放地址
        OkDownload.getInstance().getThreadPool().setCorePoolSize(3);//线程数

        //loading库 动画效果
        StyleManager s = new StyleManager();
        s.Anim(false).repeatTime(0).contentSize(-1).intercept(true);
        LoadingDialog.initStyle(s);

        //内存检漏
        //refWatcher = LeakCanary.install(this);

        ImageLoaderConfig.initImageLoader(this, BASE_IMAGE_CACHE);


        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(20000L, TimeUnit.MILLISECONDS)
                .readTimeout(20000L, TimeUnit.MILLISECONDS)
                .writeTimeout(20000L,TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);

        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .methodCount(1)         // (Optional) How many method line to show. Default 2
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

    }

//    public static RefWatcher getRefWatcher(Context context) {
//        BaseApplication application = (BaseApplication) context.getApplicationContext();
//        return application.refWatcher;
//    }





}
